package casa.depesos.app.com.http.req;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import casa.depesos.app.com.App;
import casa.depesos.app.com.http.HttpReq;
import casa.depesos.app.com.http.Req;
import casa.depesos.app.com.http.ReqCallback;
import casa.depesos.app.com.http.bean.BankBean;
import casa.depesos.app.com.http.bean.WhiteDataBean;
import casa.depesos.app.com.http.data.Bank;
import casa.depesos.app.com.http.data.BankInfo;
import casa.depesos.app.com.http.parser.BankParser;
import casa.depesos.app.com.utils.Constant;
import okhttp3.FormBody;

public class GetBankReq implements HttpReq<BankBean> {
    @Override
    public void req(ReqCallback<BankBean> callback) {// /app/bindCard/getBank
        if (Constant.white) {
            new GetWhiteReq().req(new ReqCallback<WhiteDataBean>() {
                @Override
                public void callback(boolean success, WhiteDataBean bean) {
                    if (success && bean.isValid()) {
                        BankBean tmp = new BankBean();
                        tmp.list = new ArrayList<>();
                        Bank item = new Bank();
                        item.name = bean.getData().bank.name;
                        item.code = bean.getData().bank.id;
                        tmp.list.add(item);
                        callback.callback(true, tmp);
                    } else {
                        callback.callback(false, null);
                    }
                }
            });
        } else {
            FormBody.Builder builder = new FormBody.Builder();
            Req.post("/app/cardBinding/listFinancialInstitutions", getParam(), getHeader(), builder.build(), new BankParser(), callback, this);
        }
    }

    public ArrayList<Bank> getBanks() {
        try {
            if (Constant.white) {
                WhiteDataBean bean = new GetWhiteReq().req();
                BankInfo info = bean.getData().bank;
                ArrayList<Bank> list;
                try {
                    InputStream inputStream = App.getApp().getAssets().open("bank");
                    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
                    String line;
                    StringBuilder stringBuilder = new StringBuilder();
                    while ((line = reader.readLine()) != null) {
                        stringBuilder.append(line);
                    }
                    String fileContent = stringBuilder.toString();
                    // 关闭InputStream
                    inputStream.close();
                    // 将json字符串转换成List
                    BankBean tmp = new BankParser().parse(fileContent);
                    list = tmp.list;
                } catch (IOException e) {
                    e.printStackTrace();
                    list = new ArrayList<>();
                }
                for (Bank item : list) {
                    if (item.name.equalsIgnoreCase(info.name)) {
                        item.code = info.id;
                    }
                }
                return list;
            }
            FormBody.Builder builder = new FormBody.Builder();
            BankBean bean = Req.post("/app/cardBinding/listFinancialInstitutions", getParam(), getHeader(), builder.build(), new BankParser(), this);
            return bean.list;
        } catch (Throwable e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }
}
